//
//  array+zip_all.swift
//  PokeMaster
//
//  Created by 李腾芳 on 2022/1/13.
//  Copyright © 2022 OneV's Den. All rights reserved.
//

import Foundation
import Combine

extension Array where Element: Publisher {
  var zipAll: AnyPublisher<[Element.Output], Element.Failure> {
    let initial = Just([Element.Output]())
      .setFailureType(to: Element.Failure.self)
      .eraseToAnyPublisher()
    return reduce(initial) { result, publisher in
      result.zip(publisher) { $0 + [$1] }.eraseToAnyPublisher()
    }
  }
}
